package com.yixun.qfbao.hander;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.LoginService;
import com.yixun.qfbao.apidto.LoginOutReqDto;
import com.yixun.qfbao.apidto.LoginOutResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.LoginOutReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * 退出
 * 
 * @author Administrator
 *		
 */
@Service("loginOutHander")
public class LoginOutHander extends CommonAbstractHander {
	private static Logger logger= LoggerFactory.getLogger(LoginOutHander.class);
	
	@Autowired
	private LoginUtils loginUtils;
	@Autowired
	private LoginService loginService;
	
	@SuppressWarnings("rawtypes")
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		LoginOutReqVo req = (LoginOutReqVo)reqDto;
		LoginOutReqDto reqDto1 = req.getParams();
		BaseResponseDTO<LoginOutResDto> res = new BaseResponseDTO<LoginOutResDto>();
		LoginOutResDto resDto = new LoginOutResDto();
		try {
			if (StringUtils.isNotEmpty(reqDto1.getUserId())) {
				PartnerInfo partnerInfo = loginUtils.getUser(Long.parseLong(reqDto1.getUserId()));
				if (null != partnerInfo) {
					// 更新当前用户ID的wx_openid为空
					loginService.loginOut(reqDto1);
					
					loginUtils.removeUser(Long.parseLong(reqDto1.getUserId()));
				}
			}
			
			resDto.setResult(ResultEnum.SUCCESS.getNo());
			resDto.setResultMsg("退出成功");
		} catch (Exception e) {
			logger.error("LoginOutHander error: {}", e);
			resDto.setResult("-1");
			resDto.setResultMsg("账户错误,登录失败!");
		}
		res.setResData(resDto);
		res.setResult(resDto.getResult());
		return res;
	}
	
}
